namespace ACRSAP.Issues.UserControls
{
    using System;
    using System.Diagnostics;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using ASBCC.Database.EntityFramework;
    using ASPNET.StarterKit.IssueTracker.BusinessLogicLayer;
    using ASPNET.StarterKit.IssueTracker.DataAccessLayer;
    using ASPNET.StarterKit.IssueTracker.UserInterfaceLayer;

    public class History : UserControl, IIssueTab
    {
        [AccessedThroughProperty("grdHistory")]
        private DataGrid _grdHistory;
        private int _IssueId;
        private object designerPlaceholderDeclaration;

        public History()
        {
            base.PreRender += new EventHandler(this.Page_PreRender1);
            base.Init += new EventHandler(this.Page_Init);
            this._IssueId = 0;
        }

        public void BindHistory()
        {
            this.grdHistory.DataSource = IssueHistory.GetIssueHistoryByIssueId(this._IssueId);
            this.grdHistory.DataBind();
        }

        public void grdHistoryItemDataBound(object s, DataGridItemEventArgs e)
        {
            bool flag = false;
            if ((e.Item.ItemType == ListItemType.Item) | (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                IssueHistory currentHistory = (IssueHistory)e.Item.DataItem;
                DataGrid lblDateGridData = (DataGrid)e.Item.FindControl("lblDateGrid");
                using (var dc = new CCEntities())
                {
                    var data = (from a in dc.IssueTracker_IssueHistoryValue
                                join b in dc.IssueTracker_ProjectCustomFields
                                on a.CustomFieldId equals b.CustomFieldId
                                where a.HistoryId == currentHistory.Id
                                select new
                                {
                                    Name = b.CustomFieldName,
                                    OldValue = a.OldCustomFieldValue,
                                    NewValue = a.NewCustomFieldValue
                                }).ToArray();
                    if (data != null && data.Count() != 0)
                    {
                        lblDateGridData.DataSource = data;
                        lblDateGridData.DataBind();
                    }
                }
                
                Label lblCreatorDisplayName = (Label)e.Item.FindControl("lblCreatorDisplayName");
                lblCreatorDisplayName.Text = currentHistory.CreatorDisplayName;
                Label lblDateCreated = (Label)e.Item.FindControl("lblDateCreated");
                lblDateCreated.Text = currentHistory.DateCreated.ToString("f");
                flag = true;
            }
            if (flag)
                this.grdHistory.Visible = true;
        }

        public void Initialize()
        {
        }

        [DebuggerStepThrough]
        private void InitializeComponent()
        {
            this.grdHistory.Visible = false;
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this.InitializeComponent();
        }

        private void Page_PreRender1(object sender, EventArgs e)
        {
            this.BindHistory();
        }

        int ASPNET.StarterKit.IssueTracker.UserInterfaceLayer.IIssueTab.IssueId
        {
            get
            {
                return this._IssueId;
            }
            set
            {
                this._IssueId = value;
            }
        }

        protected virtual DataGrid grdHistory
        {
            get
            {
                return this._grdHistory;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._grdHistory != null)
                {
                    this._grdHistory.ItemDataBound -= new DataGridItemEventHandler(this.grdHistoryItemDataBound);
                }
                this._grdHistory = value;
                if (this._grdHistory != null)
                {
                    this._grdHistory.ItemDataBound += new DataGridItemEventHandler(this.grdHistoryItemDataBound);
                }
            }
        }

        public int IssueId
        {
            get
            {
                return this._IssueId;
            }
            set
            {
                this._IssueId = value;
            }
        }
    }
}

